package com.ym.b2b.news.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ym.b2b.commons.controller.BaseController;
import com.ym.b2b.commons.log.CusLog;
import com.ym.b2b.news.service.EmBNewsService;
import com.ym.b2b.news.sqlbean.EmBNews;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;
import com.zc.export.ExcelUtil;
import com.zc.security.acl.domain.Users;

/***
 * @ClassName:ManagerNewsController
 * @Description: 新闻处理
 * @Author:chinavane
 * @Date: 2015年1月6日 下午12:16:03
 * @Modify:2015年1月6日 下午12:16:03
 * @CopyRight 珍诚医药在线股份有限公司
 */
@Controller
@RequestMapping("/news")
public class ManagerNewsController extends BaseController {

    @Autowired
    private EmBNewsService emBNewsService;

    // 列表查询
    @RequestMapping("searchPageNews.do")
    @ResponseBody
    public Result searchPageNews(EmBNews emBNews, Page<EmBNews> page, HttpServletRequest req) {
        Result result = new Result();
        result = emBNewsService.searchPageEmBNews(emBNews, page);
        return result;
    }

    // 导出
    @RequestMapping(value = "searchPageNewsExcel.do")
    @ResponseBody
    public Result searchPageNewsExcel(EmBNews emBNews, Page<EmBNews> page, String loadType) {
        Result ret = new Result();
        try {
            if (loadType == null || loadType.equals("")) {
                ret.setCode(Result.CODE_100);
                ret.setResult("loadType 参数 '数量'  不能为空!");
                return ret;
            }
            page.setPageable("true");
            page.setPageSize(Integer.parseInt(loadType));
            page.setStartRow(1);
            ret = emBNewsService.searchPageEmBNews(emBNews, page);
            ExcelUtil<EmBNews> excelUtil = new ExcelUtil<EmBNews>();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("model", ret.getRows());
            ret = excelUtil.createExcel("newsListExcel", map);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            ret.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return ret;
    }

    // 新增记录
    @RequestMapping("insertNews.do")
    @ResponseBody
    public Result insertNews(EmBNews emBNews, HttpServletRequest req) {
        CusLog.writeLog(getManagerTokenUser(req).getUserName() + " 新增新闻信息", "USER", 2);
        emBNews.setCreateUser(getManagerTokenUser(req).getUserName());
        Result result = new Result();
        result = emBNewsService.insertNews(emBNews);
        return result;
    }

    // 获取单条记录内容
    @RequestMapping("searchEmBNews.do")
    @ResponseBody
    public EmBNews searchEmBNews(EmBNews emBNews, HttpServletRequest req) {
        CusLog.writeLog(getManagerTokenUser(req).getUserName() + " 获取单条新闻信息: " + emBNews.getNewsId(), "USER", 2);
        EmBNews result = new EmBNews();
        result = emBNewsService.searchEmBNews(emBNews);
        return result;
    }

    // 删除记录内容
    @RequestMapping("deleteEmBNews.do")
    @ResponseBody
    public Result deleteEmBNews(EmBNews emBNews, HttpServletRequest req) {
        CusLog.writeLog(getManagerTokenUser(req).getUserName() + " 删除单条新闻信息: " + emBNews.getNewsId(), "USER", 2);
        Result result = new Result();
        try {
            emBNewsService.deleteEmBNews(emBNews.getNewsId());
        } catch (Exception e) {
            // TODO: handle exception
            result.setCode("100");
            return result;
        }
        result.setCode("200");
        return result;
    }

    // 删除多条记录内容
    @RequestMapping("deleteMultipleEmBNews.do")
    @ResponseBody
    public Result deleteMultipleEmBNews(String newsIdList, HttpServletRequest req) {
        CusLog.writeLog(getManagerTokenUser(req).getUserName() + " 删除多条新闻信息", "USER", 2);
        Result result = new Result();
        try {
            emBNewsService.deleteMultipleEmBNews(newsIdList);
        } catch (Exception e) {
            // TODO: handle exception
            result.setCode("100");
            return result;
        }
        result.setCode("200");
        return result;
    }

    // 更新记录
    @RequestMapping("updateEmBNews.do")
    @ResponseBody
    public Result updateEmBNews(EmBNews emBNews, HttpServletRequest req) throws DataAccessException {
        CusLog.writeLog(getManagerTokenUser(req).getUserName() + " 更新新闻信息: " + emBNews.getNewsId(), "USER", 2);
        Result result = new Result();
        result = emBNewsService.updateEmBNews(emBNews);
        return result;
    }

    // 批量更新记录
    @RequestMapping("updateEmBNewsBat.do")
    @ResponseBody
    public Result updateEmBNewsBat(EmBNews emBNews, HttpServletRequest req) throws DataAccessException {
        CusLog.writeLog(getManagerTokenUser(req).getUserName() + " 更新多条新闻信息", "USER", 2);
        Result result = new Result();
        result = emBNewsService.updateEmBNewsBat(emBNews);
        return result;
    }

}
